﻿using Microsoft.EntityFrameworkCore;
using ProjectCore.Common;
using ProjectCore.Domain.DomainService;
using ProjectCore.Domain.Model.Entity;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ProjectCore.Infrastructure.DomainService
{
    public class RolesDomainService : IRolesDomainService
    {
        private readonly IBaseRepository<Roles> baseRepository;
        private readonly IBaseRepository<RoleMenus> repository;
        private readonly MyContext myContext;
        public RolesDomainService(IBaseRepository<Roles> _baseRepository, MyContext _myContext, IBaseRepository<RoleMenus> _repository)
        {
            repository = _repository;
            baseRepository = _baseRepository;
            myContext = _myContext;
        }


        //添加
        public async Task<int> AddRoles(Roles roles)
        {
            try
            {
                return await baseRepository.AddEntityAsync(roles);
            }
            catch (Exception) { throw; }
        }


        //反填
        public async Task<Roles> GetRole(int Id)
        {
            try
            {
                return await myContext.Roles.FindAsync(Id);
            }
            catch (Exception) { throw; }
        }


        //获取角色权限集合
        public async Task<List<RoleMenus>> GetRoleMenus(int Id)
        {
            try
            {
                Expression<Func<RoleMenus, bool>> where = e => e.RoleID.Equals(Id);
                return await repository.WhereLoadEntityList(where);
            }
            catch (Exception)
            {
                throw;
            }
        }


        //显示
        public async Task<HeaderResult<List<Roles>>> GetRoles(int pageIndex, int pageSize)
        {
            try
            {
                Expression<Func<Roles, bool>> where = e => true;

                var roles = await baseRepository.LoadEntityListAsync(where, e => e.Sort, "asc", pageIndex, pageSize);
                int count = await baseRepository.GetEntitiesCountAsync(where);
                HeaderResult<List<Roles>> result = new HeaderResult<List<Roles>>()
                {
                    IsSucceed = true,
                    Result = roles,
                    Total = count,
                };

                return result;
            }
            catch (Exception) { throw; }
        }


        //逻辑删除
        public async Task<int> IsDel(int Id)
        {
            try
            {
                Roles roles = myContext.Roles.Where(x => x.RoleID.Equals(Id)).FirstOrDefault();
                roles.IsDeleted = true;
                return await myContext.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }

        }


        //修改角色权限
        public async Task<int> UptRoleMenu(List<int> li, int Id)
        {
            Expression<Func<RoleMenus, bool>> where = e => e.RoleID.Equals(Id);
            await repository.DelEntityAsync(where);

            List<RoleMenus> roleMenus = new List<RoleMenus>();
            for (int i = 0; i <= li.Count - 1; i++)
            {
                RoleMenus roleMenus1 = new RoleMenus();
                roleMenus1.MenuID = li[i];
                roleMenus1.RoleID = Id;
                roleMenus.Add(roleMenus1);
            }
           return await repository.AddRangeListAsync(roleMenus);
        }


        //修改
        public async Task<int> UptRoles(Roles roles)
        {
            try
            {
                return await baseRepository.UpdateEntityAsync(roles);
            }
            catch (Exception) { throw; }
        }
    }
}
